<?php

/**
 * AuthController
 * 
 * @author Maodo DIOP <dmaodo@gmail.com>
 * @version 
 */

require_once 'Zend/Controller/Action.php';
require_once 'Zend/Auth.php';
require_once 'Zend/Auth/Adapter/DbTable.php';

class Admin_TerrainController extends Zend_Controller_Action {
	
	public function indexAction() {
    	$this->_helper->viewRenderer->setNoRender();
		$this->_helper->getHelper('layout')->disableLayout();
        //$this->view->title = "Supprimer Espece";
        //$this->view->headTitle($this->view->title);
        $terrain = new Admin_Model_DbTable_Matricebr();
        $res = $terrain->getAllTerrain();
        $resultat = Zend_Json :: encode($res);
		$this->_response->setBody($resultat);
    }
	
	public function addAction() {
        $this->view->title = "Ajouter nouvel espece";
        $this->view->headTitle($this->view->title);

        $form = new Admin_Form_Terrain();
        $form->submit->setLabel('Ajouter');
        $this->view->form = $form;
        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) {         
             $matricule = $form->getValue('matricule');
             $terrain = new Admin_Model_DbTable_Matricebr();
             $terrain->addTerrain($matricule);
             $this->_helper->redirector('index');
            } else {
                $form->populate($formData);
            }
        }
    }
    
 
	public function editAction() {
        //$this->_helper->viewRenderer->setNoRender();
		//$this->_helper->getHelper('layout')->disableLayout();
        
        $form = new Admin_Form_Terrain() ;
        
        $form->submit->setLabel('Enregistrer');
        $this->view->form = $form;
        
        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) {            	

                $idterrain = (int)$this->_getParam('idterrain');;             
                $matricule = $form->getValue('matricule');           

                $terrain = new Admin_Model_DbTable_Matricebr();
                $terrain->updateTerrain($idterrain, $matricule); 
                $res = array(); 
                $res = $terrain->getTerrain($idterrain);        
                $this->_redirect('/admin/terrain/index');
            } else {
                $form->populate($formData);
            }
        } else {
        	//Si le formulaire n'est pas encore post�
        	//Recuperer la valeur de l'objet � editer
            $idterrain = $this->_request->getParam('idterrain', 0);
            if ($idterrain > 0) {
                $terrain = new Admin_Model_DbTable_Matricebr();
                $form->populate($terrain->getTerrain($idterrain));
            }
        } 
    }

    
	public function deleteAction() {
        $this->_helper->viewRenderer->setNoRender();
		$this->_helper->getHelper('layout')->disableLayout();

        $idterrain = $this->_getParam('idterrain', 0);
        $terrain = new Admin_Model_DbTable_Matricebr();
        $terrain->deleteTerrain($idterrain);
        $this->_redirect('/admin/terrain/index');
    }
    
	public function allterrainAction() {
    	$this->_helper->viewRenderer->setNoRender();
		$this->_helper->getHelper('layout')->disableLayout();
        //$this->view->title = "Supprimer Espece";
        //$this->view->headTitle($this->view->title);
        $terrain = new Admin_Model_DbTable_Matricebr();
        $res = $terrain->getAllTerrain();
        $resultat = Zend_Json :: encode($res);
		$this->_response->setBody($resultat);
    }
}

